package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.Express;
import beans.LogPay;

import utils.HibernateUtil;




public class LogPayDao {
	private SessionFactory sessionFactory;

	public LogPayDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	ExpressDao expressDao = new ExpressDao();
	
	/**
	 * 删除充值日志
	 */
	public void delete(LogPay logPay) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(logPay);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改充值日志
	 */
	public void edit(LogPay logPay) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(logPay);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的充值日志
	 */
	public void save(LogPay logPay) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(logPay);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有充值日志
	 */
	public List<LogPay> getAllLogPay(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(LogPay.class);
		List<LogPay> logPays=c.list();
		tr.commit();
		session.close();
		return logPays;
	}
	
	/**
	 * 返回流水集合
	 * 
	 * @param agent
	 * @param expresses
	 * @return
	 */
	public List<LogPay> getAllLogPay(List<Express> expresses) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(LogPay.class);
		c.add(Restrictions.in("express", expresses));
		c.add(Restrictions.eq("status", "success"));
		c.addOrder(Order.desc("time"));
		List<LogPay> logPays = c.list();
		tr.commit();
		session.close();
		return logPays;
	}
	
	/**
	 * 根据快递网点ID返回所有充值日志
	 */
	public List<LogPay> getByExpBranchID(int expBranchID){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(LogPay.class);
		c.add(Restrictions.eq("express", expressDao.getExpressBranch(expBranchID)));
		c.addOrder(Order.desc("time"));
		List<LogPay> logPays=c.list();
		tr.commit();
		session.close();
		return logPays;
	}
	/**
	 * 根据流水号获取日志
	 * @param serialNum
	 * @return
	 */
	public List<LogPay> getBySerialNum(String serialNum){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(LogPay.class);
		c.add(Restrictions.eq("serialNum", serialNum));
		List<LogPay> logPays=c.list();
		tr.commit();
		session.close();
		return logPays;
	}
	
	/**
	 * 通过包裹日志ID返回指定包裹日志
	 * 
	 * @return
	 */
	public LogPay getLogPayById(int logPayID) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		LogPay logPay = (LogPay) session.get(LogPay.class, logPayID);
		tr.commit();
		session.close();
		return logPay;
	}
}
